﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;//DOTween

public class Chessman : MonoBehaviour
{
    public Location location;//棋子的坐标
    public Animal animal;//棋子的动物类型
    public Camp camp;//棋子的阵营

    public override string ToString()
    {
        return $"棋子坐标:{location} 动物类型:{animal} 阵营:{camp}";
    }

    /// <summary>
    /// 获取当前场上的全部棋子，或者某一方的全部棋子。
    /// </summary>
    /// <param name="camp">Neutral:查询全部棋子; Blue or Red: 查询一方的全部棋子</param>
    /// <returns></returns>
    public static List<Chessman> All(Camp camp = Camp.Neutral)
    {
        List<Chessman> ret = new List<Chessman>();
        var chessmen = FindObjectsOfType<Chessman>();
        foreach (var chessman in chessmen)
        {
            if (camp == Camp.Neutral || camp == chessman.camp)
            {
                ret.Add(chessman);
            }
        }
        return ret;
    }
    /// <summary>
    /// 清除场上的全部棋子。
    /// </summary>
    public static void ClearAll()
    {
        var all = All();
        for (int i = all.Count - 1; i >= 0; i--)
        {
            all[i].ExitFromBoard();
        }
    }
    /// <summary>
    /// 依照坐标查询，找到位于相应坐标上的棋子。
    /// </summary>
    /// <param name="location"></param>
    /// <returns></returns>
    public static Chessman GetChessman(Location location)
    {
        foreach (var chessman in All())
        {
            if (chessman.location.Equals(location))
            {
                return chessman;
            }
        }
        return null;
    }
    /// <summary>
    /// 棋子所在的方格。
    /// </summary>
    public Square Square => ChessBoard.Get[location];

    /// <summary>
    /// 初始化棋子
    /// </summary>
    public void Start()
    {
        if (camp == Camp.Neutral)
        {
            Debug.LogError("棋子阵营不能为中立。");
            return;
        }
        MoveTo(location, false);
        GetComponent<Button>().onClick.AddListener(OnChessmanClicked);
    }

    public bool IsRat => animal == Animal.Rat;//棋子是否为鼠
    public bool IsElephant => animal == Animal.Elephant;//棋子是否为象
    public bool CanJump => animal == Animal.Tiger || animal == Animal.Lion;//棋子是否具有跳河能力(狮或虎)
    public int Attack => (int)animal;//棋子的强度(己方走棋时的攻击力)
    public int Defence//棋子的强度(对方走棋时的防御力，会受到陷阱的虚弱效果影响)
    {
        get
        {
            if (IsTrapped)
            {
                return 0;
            }
            return Attack;
        }
    }
    public bool IsTrapped => Square.type == SquareType.Trap && Square.camp != camp;//棋子是否处于对方陷阱中

    /// <summary>
    /// 使棋子移动到指定坐标。这会删除目标位置上的另一个棋子。
    /// </summary>
    public void MoveTo(Location target, bool swapPlayers = true)
    {
        try
        {
            Square square = ChessBoard.Get[target.x, target.y];//定位目标棋盘格
            if (square.Chessman != this)
            {
                square.RemoveChessman();//删除目标位置上已有的棋子
            }
            location = target;//修改自身坐标为新的坐标
            transform.DOMove(square.transform.position, 0.35f);//执行移动
            //transform.position = square.transform.position;//无DOTween时以此替代上一行
            if (swapPlayers)
            {
                PlayerManager.Tik();
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"移动棋子失败.{ex.Message}");
        }
    }

    private void OnChessmanClicked()
    {
        Square.OnSquareClicked();
    }

    /// <summary>
    /// 使这个棋子退场。
    /// </summary>
    public void ExitFromBoard()
    {
        Destroy(gameObject);
    }
}